﻿using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace YueRu.Common
{
    public class StringPlus
    {

        #region 字符串转换成数组
        /// <summary>把逗号隔开的字符串转换数组</summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static string[] GetStrArray(string str)
        {
            return str.Split(new char[',']);
        }
        #endregion

        /// <summary>把int数组转换以逗号隔开的字符串</summary>
        /// <param name="array">数组</param>
        /// <returns></returns>
        public static string GetString(int[] array)
        {
            var str = new StringBuilder();
            foreach (var arr in array)
            {
                str.Append(arr);
                str.Append(",");
            }

            return DelLastComma(str.ToString());
        }

        #region 删除最后一个字符之后的字符

        /// <summary>
        /// 删除最后结尾的一个逗号
        /// </summary>
        public static string DelLastComma(string str)
        {
            return str.Substring(0, str.LastIndexOf(",", System.StringComparison.Ordinal));
        }

        /// <summary>
        /// 删除最后结尾的指定字符后的字符
        /// </summary>
        public static string DelLastChar(string str, string strchar)
        {
            return str.Substring(0, str.LastIndexOf(strchar, System.StringComparison.Ordinal));
        }
        #endregion

        public static List<string> GetSubStringList(string str, char sepeater)
        {
            var ss = str.Split(sepeater);
            return ss.Where(s => !string.IsNullOrEmpty(s) && s != sepeater.ToString(CultureInfo.InvariantCulture)).ToList();
        }

        /// <summary>
        /// 转全角的函数(SBC case)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToSbc(string input)
        {
            //半角转全角：
            var c = input.ToCharArray();
            for (var i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                    c[i] = (char)(c[i] + 65248);
            }
            return new string(c);
        }

        /// <summary>
        ///  转半角的函数(SBC case)
        /// </summary>
        /// <param name="input">输入</param>
        /// <returns></returns>
        public static string ToDbc(string input)
        {
            var c = input.ToCharArray();
            for (var i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                {
                    c[i] = (char)(c[i] - 65248);
                }
            }
            return new string(c);
        }

        /// <summary>
        /// 取得一个汉字的拼音首字母
        /// </summary>
        /// <param name="cnChar">一个汉字</param>
        /// <returns>首字母</returns>
        public static string GetSpell(string cnChar)
        {
            byte[] arrCN = Encoding.Default.GetBytes(cnChar);
            if (arrCN.Length > 1)
            {
                int area = arrCN[0];
                int pos = arrCN[1];
                int code = (area << 8) + pos;
                int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
                for (int i = 0; i < 26; i++)
                {
                    int max = 55290;
                    if (i != 25) max = areacode[i + 1];
                    if (areacode[i] <= code && code < max)
                    {
                        return Encoding.Default.GetString(new[] { (byte)(65 + i) });
                    }
                }
                //生僻字
                if (cnChar == "讴")
                {
                    return "O";
                }
                return "*";
            }
            return cnChar;
        }

        #region 将字符串样式转换为纯字符串

        /// <summary>将字符串样式转换为纯字符串</summary>
        /// <param name="str"></param>
        /// <param name="split"> </param>
        /// <returns></returns>
        public static string GetCleanStyle(string str, string split)
        {
            string retrunValue;
            if (string.IsNullOrEmpty(str))
            { //如果为空，返回空值
                retrunValue = string.Empty;
            }
            else
            {
                //返回去掉分隔符
                var newString = str.Replace(str, "");
                retrunValue = newString;
            }
            return retrunValue;
        }
        #endregion

        #region 将字符串转换为新样式
        public static string GetNewStyle(string strList, string newStyle, string splitString, out string error)
        {
            //如果输入空值，返回空，并给出错误提示
            if (strList == null)
            {
                error = "请输入需要划分格式的字符串";
                return string.Empty;
            }

            //检查传入的字符串长度和样式是否匹配,如果不匹配，则说明使用错误。给出错误信息并返回空值
            var strListLength = strList.Length;
            var newStyleLength = GetCleanStyle(newStyle, splitString).Length;
            if (strListLength != newStyleLength)
            {
                error = "样式格式的长度与输入的字符长度不符，请重新输入";
                return string.Empty;
            }

            //检查新样式中分隔符的位置
            var lengstr = string.Empty;
            for (var i = 0; i < newStyle.Length; i++)
            {
                if (newStyle.Substring(i, 1) == splitString)
                {
                    lengstr = lengstr + "," + i;
                }
            }
            if (lengstr != "")
            {
                lengstr = lengstr.Substring(1);
            }
            //将分隔符放在新样式中的位置
            var str = lengstr.Split(',');
            strList = str.Aggregate(strList, (current, s) => current.Insert(int.Parse(s), splitString));

            //因为是正常的输出，没有错误
            error = string.Empty;
            return strList;
        }
        #endregion

        #region ms过滤字符串函数
        public static string InputText(string text, int maxLength)
        {
            text = text.Trim();
            if (string.IsNullOrEmpty(text))
                return string.Empty;
            if (text.Length > maxLength)
                text = text.Substring(0, maxLength);
            text = Regex.Replace(text, "[\\s]{2,}", " ");	//two or more spaces
            text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n");	//<br>
            text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " ");	//&nbsp;
            text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty);	//any other tags
            text = text.Replace("'", "''");

            return text;
        }
        /// <summary>
        /// 过滤非字母、数字加下划线
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string FilterStr(string input)
        {
            var charlist = input.ToCharArray();
            var sb = new StringBuilder();
            foreach (var item in charlist)
            {
                var result = Regex.IsMatch(item.ToString(CultureInfo.InvariantCulture), @"^[A-Za-z0-9_]*$");
                if (result)
                {
                    sb.Append(item.ToString(CultureInfo.InvariantCulture));
                }
            }
            return sb.ToString();
        }
        #endregion
    }
}
